﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using SmartSpace.Core;
using System.Web.Security;
using SmartSpace;
using SmartSapce.ServiceLocation;

namespace SmartSpace.Silverlight.Service
{
    /// <summary>
    /// Summary description for FrameworkWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class SlWebService : System.Web.Services.WebService
    {
        #region 认证

        /// <summary>
        /// 检查是否已认证
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public SingleResult<User> CheckAuthenticated()
        {
            if (User == null || !User.Identity.IsAuthenticated)
            {
                return new SingleResult<User>(false, "没有认证!", null);
            }
            else
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();
                User user = service.GetUser(this.User.Identity.Name);
                if (user != null)
                {
                    User userModel = new User();
                    userModel.Account = user.Account;
                    userModel.Name = user.Name;
                    FormsAuthentication.SetAuthCookie(user.Account, false);
                    return new SingleResult<User>(true, "认证成功!", userModel);
                }
                else
                {
                    FormsAuthentication.SignOut();
                    return new SingleResult<User>(false, "没有认证!", null);
                }
            }
        }

        /// <summary>
        /// 帐号密码认证
        /// </summary>
        /// <param name="authenticateModel">用户认证模型</param>
        /// <returns></returns>
        [WebMethod]
        public SingleResult<User> Authenticate(string account, string password)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            User user = service.GetUser(this.User.Identity.Name);
            if (user != null)
            {
                User userModel = new User();
                userModel.Account = user.Account;
                userModel.Name = user.Name;
                FormsAuthentication.SetAuthCookie(user.Account, false);
                return new SingleResult<User>(true, "认证成功!", userModel);
            }
            else
            {
                FormsAuthentication.SignOut();
                return new SingleResult<User>(false, "没有认证!", null);
            }
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public ServiceResult SignOut()
        {
            FormsAuthentication.SignOut();

            return new ServiceResult(true, "注销成功!");
        }

        #endregion

        #region 导航

        [WebMethod]
        public ListResult<Navigation> GetNavigations(Guid applicationId, string account)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            User user = service.GetUser(account);
            if (user != null)
            {
                List<Navigation> navigations = service.GetUserNavigations(applicationId, account);
                return new ListResult<Navigation>(true, "获取成功!", navigations);
            }
            else
            {
                return new ListResult<Navigation>(false, "获取失败!", null);
            }
        }

        #endregion

        #region 获取快捷方式

        [WebMethod]
        public ListResult<Shortcut> GetShortcuts(Guid applicationId, string account)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            User user = service.GetUser(account);
            if (user != null)
            {
                List<Shortcut> shortcuts = service.GetUserShortcuts(applicationId, account);
                return new ListResult<Shortcut>(true, "获取成功!", shortcuts);
            }
            else
            {
                return new ListResult<Shortcut>(false, "获取失败!", null);
            }
        }
        #endregion

        #region 应用程序信息

        [WebMethod]
        public ListResult<Application> GetApplications(string account)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            List<Application> apps = service.GetPersonalApplications("_silverlight", account);

            return new ListResult<Application>(true, "Success!", apps);
        }

        [WebMethod]
        public SingleResult<Application> GetDefaultApplication(string account)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            Application defaultApp = service.GetPersonalDefaultApplication("_silverlight", account);

            return new SingleResult<Application>(true, "Success!", defaultApp);
        }

        #endregion

        #region 主题
        [WebMethod]
        public SingleResult<Theme> GetTheme(Guid themeId)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            Theme theme = service.GetTheme(themeId);
            if (theme != null)
            {
                return new SingleResult<Theme>(true, "Success!", theme);
            }
            else
            {
                return new SingleResult<Theme>(false, "failure!", theme);
            }
        }
        #endregion

        #region 模块信息

        [WebMethod]
        public ListResult<Module> GetModules(Guid applicationId)
        {
            List<Module> Modules = new List<Module>();

            ICoreService coreService = ServiceLocator.Get<ICoreService>();
            List<Module> modules = coreService.GetModules(applicationId);
            return new ListResult<Module>(true, "获取成功!", modules);
        }

        #endregion
    }
}
